package org.pro.Model;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class ImageCache {
    private static Map<String, BufferedImage> map = new HashMap();

    public ImageCache() {
    }

    public static BufferedImage get(String key) {
        return (BufferedImage)map.get(key);
    }

    public static BufferedImage load(String key, String path) {
        BufferedImage buff = readImage(path);
        map.put(key, buff);
        return buff;
    }

    public static BufferedImage readImage(String path) {
        try {
            // 改为使用 FileInputStream 读取绝对路径
            InputStream inputStream = new FileInputStream(path);
            return ImageIO.read(inputStream);
        } catch (Exception var2) {
            var2.printStackTrace();
            return null;
        }
    }

    static {
        load("tank_blue", "D:\\javastoreage\\Tankgame\\src\\main\\resources\\assets\\tank\\bodyu1.png");
        load("tank_red", "D:\\javastoreage\\Tankgame\\src\\main\\resources\\assets\\tank\\bodyu2.png");
        load("turret_blue", "D:\\javastoreage\\Tankgame\\src\\main\\resources\\assets\\tank\\turret1_1.png");
        load("turret_red", "D:\\javastoreage\\Tankgame\\src\\main\\resources\\assets\\tank\\turret2_1.png");
        load("shot", "D:\\javastoreage\\Tankgame\\src\\main\\resources\\assets\\tank\\ammo2.png");
        load("my_shot", "D:\\javastoreage\\Tankgame\\src\\main\\resources\\assets\\tank\\ammo1.png");
        load("river", "D:\\javastoreage\\Tankgame\\src\\main\\resources\\assets\\tank\\river1.png");
        load("gross", "D:\\javastoreage\\Tankgame\\src\\main\\resources\\assets\\tank\\gross1.png");
    }
}

